layui.define(['jquery', 'layer', 'upload', 'form', 'laytpl', 'laydate', 'dropdown', 'http'], function (exports) {
    "use strict";

    var MOD_NAME = 'public',
        $ = layui.jquery,
        layer = layui.layer;
    var context = new function () {
        //打开弹窗
        this.OpenWindow = function (url, title, width = 0, height = 0) {
            var width = width + 'px';
            var height = height + 'px';
            if (width == "0px") {
                var width = ($(window).width() >= 1700 ? '1400px' : '90%');
            }
            if (height == "0px") {
                var height = ($(window).height() - 50) + 'px';
            }
            layer.open({
                type: 2,
                title: title,
                shade: 0.1,
                area: [width, height],
                content: url
            });
        };
        this.OpenAjax = function (url, title = '', rn_event = '') {
            layer.confirm('确定' + title + '数据吗？', function (index) {
                layui.http.SendAjax(url, [], [], function (res) {
                    layer.close(index);
                    if (res.code == "203") {
                        layer.msg(res.msg, {icon: 2});
                        return false;
                    } else {
                        layer.msg(res.msg, {icon: 1});
                    }
                    if (rn_event) {
                        eval(rn_event);
                    }
                })
            })
        }

        //渲染界面
        this.ViewTpl = function (data, tpl, view, type = '') {
            var getTpl = $("#" + tpl).html();
            layui.laytpl(getTpl).render(data, function (html) {
                if (type == "") {
                    $("." + view).html(html);
                } else {
                    $("." + view).append(html);
                }
                layui.form.render();
            });
        }

        this.DownFile = function (url = '') {
            layer.confirm('确定下载数据吗？', function (index) {
                layer.close(index);
                window.location.href = url;
            })
        }

        this.RandomNumber = function () {
            let randomNum = '1';
            for (let i = 0; i < 7; i++) {
                randomNum += Math.floor(Math.random() * 7);
            }
            return randomNum;
        }


        //获取参数
        this.GetQuery = function (name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return unescape(r[2]);
            }
            return '';
        }


        this.GetQueryAll = function () {
            var qs = location.search.substr(1),
                args = {}, // 保存参数数据的对象
                items = qs.length ? qs.split("&") : [],
                item = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                var name = decodeURIComponent(item[0]),
                    value = decodeURIComponent(item[1]);
                if (name) {
                    args[name] = value;
                }
            }
            return args;
        }

        this.GetUrl = function (data, url) {
            var _str = url.match(/\{(.+?)\}/g) || [];
            for (var k = 0; k < _str.length; k++) {
                var aa = _str[k].replace("{", "");
                var aa = aa.replace("}", "");
                var value = data[aa] || '';
                if (value == '') {
                    var value = layui.public.GetQuery(aa) || '';
                }
                url = url.replace(_str[k], value);
            }
            return url;
        }
        /*图片预览*/
        this.layerphotos = function (elem, type = 1) {
            if (type == 2) {
                elem = $(elem).parent('.layui-table-cell');
            }
            if (type == 3) {
                elem = $(elem).parent('.upload_list').parent('.upload_con');
            }
            layer.photos({
                photos: elem,
                anim: 5,
                shade: 0.3,
                closeBtn: 2,
                shadeClose: false
            });
        }
        this.ArrayDelMoer = function (arr = [], value = '', field = '') {
            let del = 2;
            arr.forEach(item => {
                let val = item[field] || '';
                if (value == val) {
                    del = 1;
                }
            })
            return del;
        }

        /*删除上传的文件*/
        this.delUploadFile = function (elem) {

            var that = $(elem).parent('.upload_list').parent('.upload_con').parent('.layui-input-block');
            $(elem).parent('.upload_list').remove();
            var src = "";
            $(that).find(".upload_list").each(function () {
                src += $(this).find('img').attr("src") + ',';
            })
            $(that).find('.layui-input').val(src.substr(0, src.length - 1));
            layui.layer.msg('删除成功');

        }

        this.DrawerTpl = function (elem = '', distance = '300px', title = '') {
            if ($(elem).find(".layui-card-header").length == 0) {
                var head = '<div class="layui-card-header">' +
                    title + '<i class="layui-icon layui-icon-close btnClose" style="float:right; cursor:pointer" title="关闭"></i>' +
                    '</div>';
                $(elem).prepend(head);
            }
            var dom = layui.drawer.open({
                direction: "right",
                dom: elem,
                distance: distance,
                maskClose: true
            });
            $(".btnClose").click(function () {
                dom.close();
            })
            layui.public.DrawerSearch();
            layui.public.DropdownInit(elem + ' .ruinuo_more');

        }

        this.DrawerSearch = function () {
            $(".ruinuo_drawer_search").unbind("input propertychange");
            $(".ruinuo_drawer_search").bind('input propertychange', function () {
                var val = $(this).val();
                $('.mode_list').find("li").each(function () {
                    if ($(this).text().indexOf(val) >= 0) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                })
            })
        }

        this.DrawerClick = function (elem) {
            var mode = $(elem).attr('data-mode');
            var id = $(elem).attr('data-id');
            switch (mode) {
                case "form":
                    layui.design_form.ModeFind(id);
                    break;
                case "table":
                    layui.design_table.Init(id);
                    break;
                case "tablefind":
                    layui.design_table.TableFind(id);
                    break;
                default:
            }
            $(".btnClose").click();
        }

        this.spliceArr = function (arr, len) {
            let arr_length = arr.length;
            let newArr = [];
            for (let i = 0; i < arr_length; i += len) {
                newArr.push(arr.slice(i, i + len));
            }
            return newArr;
        }

        this.getMine = function (value) {

            value = value || '';
            let files = value.split('.');
            let mime = files[files.length - 1].toLowerCase();
            return mime;
        }

        this.ChangeSql = function (where = '') {

            let str = where;
            str = str.split("@").join("d.");
            str = str.split("and").join("&&");
            str = str.split("or").join("||");
            str = str.split("=").join("==");
            return str;
        }


        this.filter = function (str) {
            str += '';
            str = str.replace(/%/g, '%25');
            str = str.replace(/\+/g, '%2B');
            str = str.replace(/ /g, '%20');
            str = str.replace(/\//g, '%2F');
            str = str.replace(/\?/g, '%3F');
            str = str.replace(/&/g, '%26');
            str = str.replace(/\=/g, '%3D');
            str = str.replace(/#/g, '%23');
            return str;
        }

        this.formateObjToParamStr = function (paramObj) {
            const sdata = [];
            for (let attr in paramObj) {
                sdata.push(`${attr}=${layui.public.filter(paramObj[attr])}`);
            }
            return sdata.join('&');
        }

        this.DropdownInit = function (elem, trigger = 'hover', dropdown_data = []) {
            if (dropdown_data.length == 0) {
                dropdown_data = $(elem + '_dropdown_data').val() || '[]';
                dropdown_data = eval(dropdown_data);
            }
            layui.dropdown.render({
                elem: elem,
                trigger: trigger,
                data: dropdown_data,
                click: function (data, othis) {
                    let obj = $(this.elem).parent('li');
                    let id = obj.attr('data-id');
                    let onclick = obj.attr('data-onclick') || '';
                    if (onclick == '') {
                        onclick = data.onclick || '';
                    }
                    let d = {id: id};
                    let url = data.url || '';
                    url = layui.public.GetUrl(d, url);
                    let title = data.title || '';
                    let width = data.width || 0;
                    let height = data.height || 0;
                    switch (data['open']) {
                        case "window":
                            layui.public.OpenWindow(url, title, width, height);
                            break;
                        case "ajax":
                            layui.public.OpenAjax(url, title, onclick);
                            break;
                    }
                },
                align: 'right',
                style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
            });
        }
    }
    exports(MOD_NAME, context);
});
